<?php
namespace app\backstage\logic;
use think\Db;

class Bank
{
    /*列表数据获取*/
    public function getList($params)
    {
        $field = 'id,agent_id,bank_name,branch_title,payee_name,bank_account,contact_person,tel,status';

        $condition[] = ['payee_name', 'like', 'require', 'payee_name'];
        $condition[] = ['bank_account', 'like', 'require', 'bank_account'];
        $condition[] = ['contact_person', 'like', 'require', 'contact_person'];
        $condition[] = ['agent_id', 'eq', 'gt:0', 'agent_id'];
        $condition[] = ['status', 'eq', 'gt:0', 'status'];

        $where = buildQueryCondition($condition, $params);

        if (request()->_role->alias == 'agent') {
            $where[] = ['agent_id', '=', request()->_user->id];
        }

        if (isset($params['field'])) {
            $orderBy = [$params['field'] => $params['order']];
        } else {
            $orderBy = ['id' => 'DESC'];
        }

        $Bank = \think\facade\App::model('Bank');
        $list = $Bank->getList($where, $field, $params['page'], $params['limit'], $orderBy);

        if ($list) {

            $agent_id_gh = array_unique(array_column($list, 'agent_id'));
            $agents = Db::table('cms_manager_user')->where('id', 'in', $agent_id_gh)->column('id,account');

            foreach ($list as $k => $v) {
                $list[$k]['agent_name'] = isset($agents[$v['agent_id']]) ? $agents[$v['agent_id']] : '';
                $list[$k]['state'] = getByBankStatusName($v['status']);
            }
        }

        $result['count'] = $Bank->getTotalNumber($where);
        $result['list'] = $list;
        return $result;
    }

    /*数据获取*/
    public function getInfo($where)
    {
        $field = 'id,agent_id,bank_name,branch_title,payee_name,bank_account,contact_person,tel,status';
        $Bank = \think\facade\App::model('Bank');

        $info = $Bank->getOne($where,$field);

        return $info;
    }

    /*数据添加*/
    public function addInfo($data)
    {
        $Bank = \think\facade\App::model('Bank');
        $res = $Bank->insertRecord($data);

        if($res){
            return ['code'=>100,'msg'=>'操作成功'];
        }

        return ['code'=>102,'msg'=>'操作失败'];
    }

    /*数据编辑*/
    public function editInfo($where,$data)
    {
        $Bank = \think\facade\App::model('Bank');
        $res = $Bank->updateRecord($where,$data);

        if($res){
            return ['code'=>100,'msg'=>'操作成功'];
        }

        return ['code'=>102,'msg'=>'操作失败'];
    }

    /*审核*/
    public function examineInfo($where,$data)
    {
        $Bank = \think\facade\App::model('Bank');
        $res = $Bank->updateRecord($where,$data);

        if($res){
            return ['code'=>100,'msg'=>'操作成功'];
        }

        return ['code'=>102,'msg'=>'操作失败'];
    }

    /*数据处理*/
    public function dataProcessing($params)
    {
        if(request()->_role->alias == 'agent'){
            $data['agent_id']       = request()->_user->id;
        }

        $data['bank_name']      = $params['bank_name'];
        $data['branch_title']   = $params['branch_title'];
        $data['payee_name']     = $params['payee_name'];
        $data['bank_account']   = $params['bank_account'];

        if(isset($params['contact_person']))
        {
            $data['contact_person']   = $params['contact_person'];
        }

        if(isset($params['tel']))
        {
            $data['tel']   = $params['tel'];
        }

        return ['data'=>$data];
    }

}